#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "Image.h"
#include <QMenuBar>
#include <QFileDialog>
#include <QString>
#include <string>
#include <QDebug>
#include <QPainter>
#include <QPaintDevice>
#include <QPaintEvent>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initImg = NULL;
    img = NULL;
    isFileOpen=false;

    //菜单栏
    QMenuBar * mBar = menuBar();
    setMenuBar(mBar);

    //文件菜单模块
    QMenu *fileMenu = mBar->addMenu("文件");
    QAction *openFile = fileMenu->addAction("打开文件");
    fileMenu->addSeparator();
    QAction *saveFile = fileMenu->addAction("保存文件");
    QAction *closeFile = fileMenu->addAction("关闭文件");

    //编辑菜单模块
    QMenu *editMenu = mBar->addMenu("编辑");
    QAction *undoEdit = editMenu->addAction("撤销");
    QAction *resetEdit = editMenu->addAction("重置");

    //功能菜单模块
    QMenu *funcMenu = mBar->addMenu("功能");

    QMenu *flipmenu = funcMenu->addMenu("翻转");
    QAction *fliplrFunc = flipmenu->addAction("左右翻转");
    QAction *flipudFunc = flipmenu->addAction("上下翻转");

    QMenu *rotatemenu = funcMenu->addMenu("旋转");
    QAction *rotateFunc = rotatemenu->addAction("顺时针旋转90度");
    QAction *anti_rotateFunc = rotatemenu->addAction("逆时针旋转90度");

    QAction *cropFunc = funcMenu->addAction("裁剪");

    //风格菜单模块
    QMenu *styleMenu = mBar->addMenu("风格");
    QAction *grayStyle = styleMenu->addAction("灰度化");
    QAction *handStyle = styleMenu->addAction("手绘风");


    //文件链接
    //打开文件的连接
    connect(openFile,&QAction::triggered,[=](){
       QString filePath = QFileDialog::getOpenFileName(this,"open file");
       if(Image::FileOpen(filePath.toStdString(),bmp)){
            qDebug()<<"read success!";
            img = new Image;
            initImg = new Image;
            *img = Image::ReadBmp(bmp);
            *initImg = *img;
            isFileOpen = true;//更改状态
            update();//更新绘图事件
       }else{
           //弹出提示框 文件打开失败
          qDebug()<<"file open error!";
       }

    });

    //保存文件的连接
    connect(saveFile,&QAction::triggered,[=](){
        if(isFileOpen){
            QString savePath = QFileDialog::getSaveFileName(this,"save file");
            img->WriteBmp(savePath.toStdString());
        }else{
            //弹出提示框 文件未打开
            qDebug()<<"file is not open!";
        }

    });

    //关闭文件的链接
    connect(closeFile,&QAction::triggered,[=](){
        if(isFileOpen){
            img = NULL;
            isFileOpen = false;
            update();
            bmp.close();
        }else{
            //弹出提示框 文件未打开
            qDebug()<<"file is not open!";
        }

    });

    //编辑链接
    connect(undoEdit,&QAction::triggered,[=](){
        if(!undoStack.empty()){
            delete img;
            img = undoStack.top();
            undoStack.pop();
            update();
        }else{
            qDebug()<<"already init status\n";
            //弹出警告框
        }
    });

    connect(resetEdit,&QAction::triggered,[=](){
        if(isFileOpen){
            *img = *initImg;
            while(!undoStack.empty()){
                Image*tmp = undoStack.top();
                delete tmp;
                undoStack.pop();
            }
            update();
        }else{
            qDebug()<<"file is not opened!\n";
            //弹出警告框
        }
    });

    //功能链接
    connect(fliplrFunc,&QAction::triggered,[=](){
        if(isFileOpen){
            Image *tmp = new Image;
            *tmp = *img;
            undoStack.push(tmp);

            img->Flip();
            update();
        }else{
            qDebug()<<"file is not opened!\n";
            //弹出警告框
        }
    });

    connect(flipudFunc,&QAction::triggered,[=](){
        if(isFileOpen){
            Image *tmp = new Image;
            *tmp = *img;
            undoStack.push(tmp);

            img->Flip(1);
            update();
        }else{
            qDebug()<<"file is not opened!\n";
            //弹出警告框
        }
    });

    connect(rotateFunc,&QAction::triggered,[=](){
        if(isFileOpen){
            Image *tmp = new Image;
            *tmp = *img;
            undoStack.push(tmp);

            img->Rotate(90);
            update();
        }else{
            qDebug()<<"file is not opened!\n";
            //弹出警告框
        }
    });

    connect(anti_rotateFunc,&QAction::triggered,[=](){
        if(isFileOpen){
            Image *tmp = new Image;
            *tmp = *img;
            undoStack.push(tmp);

            img->Rotate(270);
            update();
        }else{
            qDebug()<<"file is not opened!\n";
            //弹出警告框
        }
    });


    connect(cropFunc,&QAction::triggered,[=](){
        if(isFileOpen){
            Image *tmp = new Image;
            *tmp = *img;
            undoStack.push(tmp);

            img->Crop(0,0,img->getH()/2,img->getW()/2);
            update();
        }else{
            qDebug()<<"file is not opened!\n";
            //弹出警告框
        }
    });

    connect(grayStyle,&QAction::triggered,[=](){
        if(isFileOpen){
            Image *tmp = new Image;
            *tmp = *img;
            undoStack.push(tmp);

            img->Gray();
            update();
        }else{
            qDebug()<<"file is not opened!\n";
            //弹出警告框

        }
    });

    connect(handStyle,&QAction::triggered,[=](){
        if(isFileOpen){
            Image *tmp = new Image;
            *tmp = *img;
            undoStack.push(tmp);

            img->HandStyle();
            update();
        }else{
            qDebug()<<"file is not opened!\n";
            //弹出警告框

        }
    });

}

MainWindow::~MainWindow()
{
    delete ui;
    if(img!=NULL){
        delete img;
    }

    if(initImg!=NULL){
        delete initImg;
    }
}

void MainWindow::paintEvent(QPaintEvent*){
    if(isFileOpen){
        QPainter painter(this);
        QRect target(200,100,400,400);//初设需要修改 -- 设置中心位置
        painter.drawImage(target,this->img->ToQImage());
    }
}
